<?php

class Fpt_Business_Block_block_adapter_mysql extends Fpt_Business_Block_block_adapter_abstract
{

    /**
     *
     * @return Fpt_Business_Block_block_adapter_mysql
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }
 
 

    public function addBlock($blockParams)
    {
        try
        {
            $rs = false;
           
            $objDB = Fpt_Global::getDB('block');
            // Prepare sql

            $stmt = $objDB->prepare('Call sp_insertBlock(:p_name, :p_title, :p_desc,
                :p_module, :p_controller, :p_action,:p_params, @p_result);');
            // Bind params
            $stmt->bindParam('p_name', $blockParams['BlockName'], PDO::PARAM_STR);
            $stmt->bindParam('p_title', $blockParams['BlockTitle'], PDO::PARAM_STR);
            $stmt->bindParam('p_desc', $blockParams['BlockDesc'], PDO::PARAM_STR);
            $stmt->bindParam('p_module', $blockParams['BlockModule'], PDO::PARAM_STR);
            $stmt->bindParam('p_controller', $blockParams['BlockController'], PDO::PARAM_STR);
            $stmt->bindParam('p_action', $blockParams['BlockAction'], PDO::PARAM_STR);
            $stmt->bindParam('p_params', $blockParams['BlockParams'], PDO::PARAM_STR);

            // execute
            $stmt->execute();
            
            // Close cursor
            $stmt->closeCursor();

            $stmt = $objDB->query("SELECT @p_result");
            
            $rs = $stmt->fetchColumn();
      
            if(intval($rs) > 0)
            {
                $rs = true;
            }
            // Release
            unset($stmt);
        }
        catch (Exception $ex)
        {
            //log error
            echo $ex->getMessage();
        }
        catch (Zend_Db_Exception $ex)
        {
            //log err
            echo $ex->getMessage();
        }
        return $rs;
    }

    public function getBlock($params = array('BlockName'=>null, 'BlockStatus'=>null))
    {
        try
        {
            $rs = false;
            
            $objDB = Fpt_Global::getDB('block');
            // Prepare sql
            $stmt = $objDB->prepare('Call sp_getBlockByName(:p_name, :p_status);');
            // Bind params
            $stmt->bindParam('p_name', $params['BlockName']);
            $stmt->bindParam('p_status', $params['BlockStatus']);
            // execute
            $stmt->execute();
            
            $rs = $stmt->fetchAll();
          
            // Close cursor
            $stmt->closeCursor();

            // Release
            unset($stmt);
        }
        catch (Exception $ex)
        {
            //log error
            echo $ex->getMessage();
        }
        catch (Zend_Db_Exception $ex)
        {
            //log err
            echo $ex->getMessage();
        }
        return $rs;
    }
    
    
    public function getListBlock($offset = null, $limit = null)
    {
        try
        {
            $rs = false;
            $objDB = Fpt_Global::getDB('block');
            // Prepare sql
            $stmt = $objDB->prepare('Call sp_getListBlock(:p_offset, :p_limit, @p_rowcount);');
            // Bind params
            $stmt->bindParam('p_offset', $offset, PDO::PARAM_INT);
            $stmt->bindParam('p_limit', $limit, PDO::PARAM_INT);

            // execute
            $stmt->execute();
            
            $rs['data'] = $stmt->fetchAll();
            
            // Close cursor
            $stmt->closeCursor();

            $stmt = $objDB->query("SELECT @p_rowcount");
            
            $rs['total'] = $stmt->fetchColumn();
            
            // Release
            unset($stmt);
        }
        catch (Exception $ex)
        {
            //log error
            echo $ex->getMessage();
        }
        catch (Zend_Db_Exception $ex)
        {
            //log err
            echo $ex->getMessage();
        }
        return $rs;
    }
}